clear all;
close all;clc
Subjects = {'A63','A64','A68','A69'};
% 'A63','A64','A68','A69','A70','A72','A73','A74','A75','A76','A77','A78','A79','A80','A81','A82'
fs = 500;
Q = 25;
fs_low = fs/Q;
dt = 1/fs;
[b,a]=fir1(200,[0.1 2.5]./(fs/2));
f_Conditions_EMG_EEG_up
fingers = 'abcde';
pcolor = 'kbr';
Pre = 0; % on
dur = 3.5; % off
dur_int = 1;
for i=1:round(3/dur_int)
    times(i,:)= (i-1)*dur_int+[0 dur_int];
end
times
tsig_ori=Pre+dt:dt:dur;
tsig_plot=Pre+dt*Q:dt*Q:dur;
NN = length(tsig_ori);
plot_triggers = 0;
ifnumber = 1;
kernal = 5;
Ntrials = 50;
N_points = 200;
Nrow = 12;
[st1 st2]=size(times);
timesp = round(times.*fs);
figure('position',[100 80 800 250]);
for isub = 1:length(Subjects)
    Sub = Subjects{isub}
    finger_names = {'Thumb';'Index';'Middle';'Ring';'Pinky'};
    eval(['condall=' Sub '_all;']);
    outcor = f_heatmap_subplot(length(Subjects),st1);
    for jc_channel = 1:31
        for ifinger = 1:5
            ERP{ifinger,jc_channel}= [];
        end
    end
    %     for isession = 1:N_column:length(condall)
    for isession = 1:N_column:20*5
        chunkline = {condall{isession:isession+N_column-1}}
        finger = chunkline{1}(end);
        ifinger = find(fingers==finger);
        fname = ['EEG_data\' chunkline{2} ];
        sample1 = 1;
        sample2= 10;
        eeg = read_eep_cnt(fname, sample1, sample2);
        sample2 = eeg.nsample;
        eeg = read_eep_cnt(fname, sample1, sample2);
        eval(['load ' fname(1:end-4) '_P']);
        Nchannel = eeg.nchan;
        tsig_long = eeg.time./1000;
        % ***** triggers & PRHs (period histograms)
        % first, get the behavioral file:
        outTT = f_process_trigger(eeg,chunkline{3},plot_triggers,20);
        newTR_time = outTT.newTR_time;
        %             stop0
        if length(newTR_time)<20
            warning_less_than_20
        end
        intervals = diff(newTR_time);
        new_data2=f_montage(new_data);
        for jc_channel = 1:31
            xdata = filter(b,a,new_data2(jc_channel,:));
            %                         newTR_time=newTR_time(1:min([length(newTR_time) 90]));
            for i = 1:length(newTR_time) %  all triggers
                Ttime = newTR_time(i);
                [YY Ind] = min(abs(Ttime-tsig_long));
                chunk = xdata(Ind+round((1+Pre)*fs):Ind+round((1+Pre)*fs)+NN-1);
                %             ERP{ifinger,jc_channel} =[
                %                 ERP{ifinger,jc_channel}
                %                 resample(chunk,1,Q)];
                ERP{ifinger,jc_channel} =[
                    ERP{ifinger,jc_channel}
                    chunk];
            end % for
        end
    end
    for itime = 1:st1
        for jc_channel=1:31
            for ifinger = 1:5
                longv = ERP{ifinger,jc_channel}(:,max([1 timesp(itime,1)]):timesp(itime,2));
                [dd1 dd2]=size(longv);
                for jk = 1:dd1
                    ALL_vectors{ifinger}(jk,:) = resample(longv(jk,:),1,Q);
                    
                end
            end
            
            pause(0.1)
                        out = f_lda_single(ALL_vectors);
%             out =f_lda_multi(ALL_vectors,kernal,Ntrials,N_points);
            %                         out = f_lda_multi_neariest_neighbor(ALL_vectors,kernal,Ntrials,N_points,37)
            PC(jc_channel) = out.PC*100
            if isub == 1 & itime == 2 & jc_channel==16 % Cz
                CM = out.CM;
            end
            clear ALL_vectors
        end
        
        subplot('position',outcor(itime+(isub-1)*st1,:));
        f_plot_heatmap(31,PC,0,40,1)
        colormap(jet)
        clear PC
        c = colorbar;
        pause(0.1)
    end

end
figure;
maxV = 40;
f_plotCM(CM,maxV)
title("Electrode Cz");
PC_fingers = diag(CM);

figure;
bar(PC_fingers)
ylabel('Accuracy (%)')
xlabel('Fingers')
set(gca,'xtick',1:5,'xticklabel',finger_names)
hold on;
plot([1 5],[20 20],'r');
title("Electrode Cz");
ylim([0 60]);

